package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class TrademarkController {

    //PaginationInnerInterceptor

    @Autowired
    BaseTrademarkService baseTrademarkService;

    /**
     * 分页查询所有品牌
     * @param page   页码
     * @param limit  每页几个
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result getBaseTrademark(@PathVariable("page")Long page,
                                   @PathVariable("limit")Long limit){


        //mybatis-plus
        //1、构造一个Page 对象，传入页码等信息
        //2、service调用page(传入page对象)分页方法即可
        Page<BaseTrademark> trademarkPage = new Page<>(page,limit);
        Page<BaseTrademark> data = baseTrademarkService.page(trademarkPage);
//        List<BaseTrademark> records = data.getRecords();
        return Result.ok(data);
    }


    /**
     * 品牌保存
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){

        baseTrademarkService.save(baseTrademark);
        return Result.ok();

    }

    /**
     * 根据id获取品牌
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademarkById(@PathVariable("id")Long id){
        BaseTrademark trademark = baseTrademarkService.getById(id);
        return Result.ok(trademark);
    }


    /**
     * 修改品牌
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){

        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result removeBaseTrademark(@PathVariable("id")Long id){

        baseTrademarkService.removeById(id);
        return Result.ok();
    }


    /**
     * 获取所有品牌信息，方便在录入spu的时候进行选择
     * /baseTrademark/getTrademarkList
     * @return
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarks(){
        //获取所有品牌
        List<BaseTrademark> list = baseTrademarkService.list();

        return Result.ok(list);
    }






}
